package operation;

import book.Book;
import book.BookList;
import user.User;

import java.util.Scanner;

public class AddOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("新增图书！");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();

        System.out.println("请输入作者:");
        String author = scanner.nextLine();

        System.out.println("请输入类型:");
        String type = scanner.nextLine();

        // 把价格放在最后输入,因为价格会有一个回车,
        // 会被下一个scanner读到,造成没有输入下一个数据
        System.out.println("请输入价格:");
        int price = scanner.nextInt();



        // 检查数组当中有没有这本书
        int useSize = bookList.getUsedSize();
        for(int i = 0;i < useSize;i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println("有这本书了，不进行存放了!");
                return;
            }
        }

        if(useSize == bookList.getBooks().length){
            System.out.println("书架满了!");
        }
        else {
            Book book = new Book(name, author, price, type);
            bookList.setBook(useSize, book);
            // useSize要改变,要加1
            bookList.setUsedSize(useSize+1);
        }
    }
}
